<include file="Index/_meta" />
<body>
<div class="layui-fluid" style="margin-top: 20px">
    <article class="page-container">
        <form action="/index.php/Admin/Role/add" method="post" class="form form-horizontal" id="form-admin-role-add">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>所管辖区域：</label>
                <div class="formControls col-xs-8 col-sm-9"> <span class="select-box">
                    <select class="select" name="jibie" size="1">
                        <option value="0">请选择该管理员所能管辖的区域</option>
                        <?php foreach($roleData as $v){?>
                        <option value="<?php echo $v['id']?>">
                        <?php echo str_repeat('-',8*$v['level']),$v
['name']?>
                    </option>
                        <?php }?>
                    </select>
		        </span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="请输入角色名称" id="roleName" name="role_name" datatype="*4-16">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">角色所具有的权限：</label>
                <div class="formControls col-xs-8 col-sm-9">

                    <?php foreach($priData as $k=>$v){?>
                    <?php if($v['level']==0):?>
                    <dl class="permission-list">
                        <dt>
                            <label>
                                <input type="checkbox" value="<?php echo $v['id']?>" name="pri_id[]">
                                <?php echo $v['pri_name']?></label>
                        </dt>

                        <?php foreach($priData as $k1=>$v1):?>
                        <?php if($v['id']==$v1['parent_id']):?>
                        <dd>
                            <dl class="cl permission-list2">
                                <dt>
                                    <label class="">
                                        <input pane="" type="checkbox" value="<?php echo $v1['id']?>" name="pri_id[]">
                                        <?php echo $v1['pri_name']?></label>
                                </dt>
                                <dd>
                                    <?php foreach($priData as $k2=>$v2):?>
                                    <?php if($v1['id']==$v2['parent_id']):?>
                                    <label class="">
                                        <input type="checkbox" value="<?php echo $v2['id']?>" name="pri_id[]">
                                        <?php echo $v2['pri_name']?></label>
                                    <?php endif;?>
                                    <?php endforeach;?>
                                </dd>
                            </dl>
                        </dd>
                        <?php endif;?>
                        <?php endforeach;?>
                    </dl>
                    <?php endif; ?>
                    <?php }?>
                    </dd>
                    </dl>
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <button type="button" id="tijiao" class="btn btn-primary size-L radius" id="admin-role-save"><i class="icon-ok"></i> 确定提交</button>
                </div>
            </div>
        </form>
    </article>
</div>
</body>
<script type="text/javascript" src="__PUBLIC__/frame/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['form', 'layer'], function () {
        // 操作对象
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery;

        $("#tijiao").click(function () {
            var role_Name = $("#roleName").val();
            if(role_Name==''){
                layer.msg('角色名称不能为空');
                return false;
            }
            $.post("/index.php/home/role/add", $('form').serialize(),
                function(result){
                    if(result.status == '0'){
                        layer.alert(result.message);
                    }
                    if(result.status == '1'){
                        layer.open({
                            content:result.message,
                            icon:1,
                            yes:function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.$('.refresh').click();  //添加成功获取一下数据
                                parent.layer.close(index); //再执行关闭
                            }
                        });
                    }
                },
                "JSON");
        })



        $(".permission-list dt input:checkbox").click(function(){
            $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function(){
            var l =$(this).parent().parent().find("input:checked").length;
            var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if($(this).prop("checked")){
                $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
            }
            else{
                if(l==0){
                    $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
                }
                if(l2==0){
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
                }
            }
        });

    })
</script>